﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet4
    {
        private void Sheet4_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet4_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet4_Startup);
            this.Shutdown += new System.EventHandler(Sheet4_Shutdown);
        }

        #endregion

        private static readonly int POS_GameNameCol = 2;
        private static readonly int POS_GameNameRow = 1;
        private static readonly int POS_TeamCountCol = 2;
        private static readonly int POS_TeamCountRow = 2;
        private static readonly int POS_RoundNoCol = 2;
        private static readonly int POS_RoundNoRow = 3;
        private static readonly int POS_EntryRow = 5;
        private static readonly int POS_InCourceBibCol = 1;
        private static readonly int POS_InCourceTeamNameCol = 2;
        private static readonly int POS_OutCourceBibCol = 3;
        private static readonly int POS_OutCourceTeamNameCol = 4;
        private static readonly int POS_CourceNameRow = 4;
        private static readonly int POS_InCourceNameCol = 1;
        private static readonly int POS_OutCourceNameCol = 4;
        private static readonly int POS_AreaNameCol = 5;
        private static readonly int POS_AreaNameRow = 1;

        private static int CPOS_InCourceBibCol = 1;
        private static int CPOS_InCourceTeamNameCol = 2;
        private static int CPOS_InCourceIDOfTeam = 3;
        private static int CPOS_OutCourceBibCol = 4;
        private static int CPOS_OutCourceTeamNameCol = 5;
        private static int CPOS_OutCourceIDOfTeam = 6;

        private static readonly string LABEL_InCourceName = @"インコース";
        private static readonly string LABEL_OutCourceName = @"アウトコース";
        public void ReverseCource()
        {
            if (CPOS_InCourceBibCol == POS_InCourceBibCol)
            {
                ((Excel.Range)Globals.Sheet4.Cells[POS_CourceNameRow, POS_InCourceNameCol]).Value2 = LABEL_OutCourceName;
                ((Excel.Range)Globals.Sheet4.Cells[POS_CourceNameRow, POS_OutCourceNameCol]).Value2 = LABEL_InCourceName;
                CPOS_InCourceBibCol = POS_OutCourceBibCol;
                CPOS_InCourceTeamNameCol = POS_OutCourceTeamNameCol;
                CPOS_OutCourceBibCol = POS_InCourceBibCol;
                CPOS_OutCourceTeamNameCol = POS_InCourceTeamNameCol;
            }
            else
            {
                ((Excel.Range)Globals.Sheet4.Cells[POS_CourceNameRow, POS_InCourceNameCol]).Value2 = LABEL_InCourceName;
                ((Excel.Range)Globals.Sheet4.Cells[POS_CourceNameRow, POS_OutCourceNameCol]).Value2 = LABEL_OutCourceName;
                CPOS_InCourceBibCol = POS_InCourceBibCol;
                CPOS_InCourceTeamNameCol = POS_InCourceTeamNameCol;
                CPOS_OutCourceBibCol = POS_OutCourceBibCol;
                CPOS_OutCourceTeamNameCol = POS_OutCourceTeamNameCol;
            }
        }

        public enum Side
        {
            Left,
            Right
        }
        public void SetTeamInfo(string Id, string bib, string name, int turn, Side side)
        {
            int posBib = CPOS_InCourceBibCol;
            int posName = CPOS_InCourceTeamNameCol;
            int posId = CPOS_InCourceIDOfTeam;
            if (side == Side.Right)
            {
                posBib = CPOS_OutCourceBibCol;
                posName = CPOS_OutCourceTeamNameCol;
                posId = CPOS_OutCourceIDOfTeam;
            }
            ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + turn, posBib]).Value2 = bib;
            ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + turn, posId]).Value2 = Id;
            ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + turn, posName]).Value2 = name;
        }

        public int GetTeamNo()
        {
            string val = ((Excel.Range)Globals.Sheet4.Cells[POS_TeamCountRow, POS_TeamCountCol]).Value2.ToString();
            int no = 0;
            if (Int32.TryParse(val, out no))
            {
                return no;
            }
            return no;
        }

        public string GetGameName()
        {
            if (((Excel.Range)Globals.Sheet4.Cells[POS_GameNameRow, POS_GameNameCol]).Value2 != null)
            {
                return ((Excel.Range)Globals.Sheet4.Cells[POS_GameNameRow, POS_GameNameCol]).Value2.ToString();
            }
            return null;
        }

        public void SetGameName(string name)
        {
            ((Excel.Range)Globals.Sheet4.Cells[POS_GameNameRow, POS_GameNameCol]).Value2 = name;
        }

        public string GetAreaName()
        {
            if (((Excel.Range)Globals.Sheet4.Cells[POS_AreaNameRow, POS_AreaNameCol]).Value2 != null)
            {
                return ((Excel.Range)Globals.Sheet4.Cells[POS_AreaNameRow, POS_AreaNameCol]).Value2.ToString();
            }
            return null;
        }

        public void SetAreaName(string name)
        {
            ((Excel.Range)Globals.Sheet4.Cells[POS_AreaNameRow, POS_AreaNameCol]).Value2 = name;
        }

        public EntryTurnInfo[] GetEntryTurnInfos()
        {
            List<EntryTurnInfo> list = new List<EntryTurnInfo>();
            int no = GetTeamNo();
            for (int i = 1; i <= no; i++)
            {
                int? icBib=null;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceBibCol]).Value2 != null)
                {
                    string val = null;
                    val = ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceBibCol]).Value2.ToString();
                    int bibVal;
                    if (Int32.TryParse(val, out bibVal))
                    {
                        icBib = bibVal;
                    }
                }
                string icTeamName = null;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceTeamNameCol]).Value2 != null)
                {
                    icTeamName= ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceTeamNameCol]).Value2.ToString();

                }
                int? ocBib=null;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceBibCol]).Value2 != null)
                {
                    string val = ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceBibCol]).Value2.ToString();
                    int bibVal;
                    if (Int32.TryParse(val, out bibVal))
                    {
                        ocBib = bibVal;
                    }
                }
                string ocTeamName = null;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceTeamNameCol]).Value2 != null)
                {
                    ocTeamName= ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceTeamNameCol]).Value2.ToString();
                }

                string icIdOfTeam = null;
                long icRegIdOfTeam = -1;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceIDOfTeam]).Value2 != null)
                {
                    icIdOfTeam = ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_InCourceIDOfTeam]).Value2.ToString();
                    icRegIdOfTeam = long.Parse(icIdOfTeam);
                }

                string ocIdOfTeam = null;
                long ocRegIdOfTeam = -1;
                if (((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceIDOfTeam]).Value2 != null)
                {
                    ocIdOfTeam = ((Excel.Range)Globals.Sheet4.Cells[POS_EntryRow + i, CPOS_OutCourceIDOfTeam]).Value2.ToString();
                    ocRegIdOfTeam = long.Parse(ocIdOfTeam);
                }

                list.Add(new EntryTurnInfo { ICBib = icBib, ICTeamName = icTeamName, OCBib = ocBib, OCTeamName = ocTeamName, ICIdOfTeam = icIdOfTeam, OCIdOfTeam = ocIdOfTeam, ICRegIdOfTeam = icRegIdOfTeam, OCRegIdOfTeam = ocRegIdOfTeam });
            }
            return list.ToArray();
        }

        public void SetTurnNo(int turnNo)
        {
            ((Excel.Range)Globals.Sheet4.Cells[POS_TeamCountRow, POS_TeamCountCol]).Value2 = turnNo;
        }

        public void SetRoundNo(int roundNo)
        {
            ((Excel.Range)Globals.Sheet4.Cells[POS_RoundNoRow, POS_RoundNoCol]).Value2 = roundNo;
        }

    }

    public class EntryTurnInfo
    {
        public int? ICBib { get; set; }
        public string ICTeamName { get; set; }
        public bool? IsICExhibition { get; set; }
        public int? OCBib { get; set; }
        public string OCTeamName { get; set; }
        public bool? IsOCExhibition { get; set; }
        public string ICIdOfTeam { get; set; }
        public string OCIdOfTeam { get; set; }
        public long ICRegIdOfTeam { get; set; }
        public long OCRegIdOfTeam { get; set; }
    }

}
